/*
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2023-10-13 15:13:04
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2024-09-03 22:57:35
 * @FilePath: /VoxGS/packages/PureK/src/convention.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

export function toUpperCase(target) {
  return String.prototype.toUpperCase.call(target);
}

export function toLowerCase(target) {
  return String.prototype.toLowerCase.call(target);
}

// // Hungarian strUserName
// ['str', 'User', 'Name'].join('')
// // Camel userName
// ['user', 'Name'].join('')
// // Pascal UserName
// ['User', 'Name'].join('')
// // Snake user_name
// ['user', 'name'].join('_')

export function HungarianToCamel(target) {
  const [, ...list] = target.split(/(?=[A-Z])/g);
  list[0] = toLowerCase(list[0]);
  return list.join("");
}
export function HungarianToPascal(target) {
  const [, ...list] = target.split(/(?=[A-Z])/g);
  return list.join("");
}
export function HungarianToSnake(target) {
  const [, ...list] = target.split(/(?=[A-Z])/g);
  return list.map(toLowerCase).join("_");
}

export function CamelToPascal(target) {
  const list = target.split(/(?=[A-Z])/g);
  return list
    .map((item) => item.replace(/\b\w/g, ($0) => $0.toUpperCase()))
    .join("");
}
export function CamelToSnake(target) {
  const list = target.split(/(?=[A-Z])/g);
  return list
    .map((item) => item.replace(/\b\w/g, ($0) => $0.toLowerCase()))
    .join("_");
}

export function PascalToCamel(target) {
  const list = target.split(/(?=[A-Z])/g);
  list[0] = list[0].replace(/\b\w/g, ($0) => $0.toLowerCase());
  return list.join("");
}
export function PascalToSnake(target) {
  const list = target.split(/(?=[A-Z])/g);
  return list.map(toLowerCase).join("_");
}

export function SnakeToCamel(target) {
  const list = target.split(/\_/g);
  return list
    .map((item, index) =>
      index === 0 ? item : item.replace(/\b\w/g, ($0) => $0.toUpperCase())
    )
    .join("");
}
export function SnakeToPascal(target) {
  const list = target.split(/\_/g);
  return list
    .map((item) => item.replace(/\b\w/g, ($0) => $0.toUpperCase()))
    .join("");
}

export const MEANS = {
  HungarianToCamel,
  HungarianToPascal,
  HungarianToSnake,
  CamelToPascal,
  CamelToSnake,
  PascalToCamel,
  PascalToSnake,
  SnakeToCamel,
  SnakeToPascal,
};

function isType(type, target) {
  return Object.prototype.toString.call(target) === `[object ${type}]`;
}

export function objKeyTransfer(target, manner) {
  return Object.keys(target).reduce((acc={}, key) => {
    const value = target[key];
    let result;
    if(isType("Object", value)) {
      result = objKeyTransfer(value, manner);
    }else if(isType("Array", value)) {
      result = value.map(item => {
        if(isType("Object", item)) {
          return objKeyTransfer(item, manner)
        } else  {
          return item;
        }
      });
    }else {
      result = value;
    }



    return {
      ...acc,
      [manner(key)]: result,
    };
  }, {});
}
